package com.jprocms.module.cms.service.channel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.jpro.framework.common.util.collection.CollectionUtils;
import com.jprocms.module.cms.constant.CmsSysConstants;
import com.jprocms.module.cms.controller.admin.channel.dto.ChannelRuleDto;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelExtDO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelRuleMapDO;
import com.jprocms.module.cms.dal.dataobject.content.ContentDO;
import com.jprocms.module.cms.dal.mysql.channel.ChannelExtMapper;
import com.jprocms.module.cms.dal.mysql.channel.ChannelRuleMapMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.CHANNEL_NOT_EXISTS;

/**
 * 栏目扩展配置 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class ChannelExtServiceImpl implements ChannelExtService {

    @Resource
    private ChannelExtMapper channelExtMapper;
    @Resource
    private ChannelRuleMapMapper channelRuleMapMapper;

    private void validateChannelExtExists(Long id) {
        if (channelExtMapper.selectById(id) == null) {
            throw exception(CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public ChannelExtDO getChannelExt(Long id) {
        return channelExtMapper.selectById(id);
    }

    @Override
    public List<ChannelRuleDto> getAutoPushChannelRule(ContentDO contentDO) {
        List<ChannelRuleMapDO> channelRuleMapDOS = channelRuleMapMapper.selectList(ChannelRuleMapDO::getSourceId, contentDO.getChannelId());
        if (CollUtil.isEmpty(channelRuleMapDOS)) {
            return null;
        }
        List<Long> targetChannelIds = CollectionUtils.convertList(channelRuleMapDOS, ChannelRuleMapDO::getTargetId);
        List<ChannelExtDO> channelExtDOS = channelExtMapper.selectBatchIds(targetChannelIds);
        if (CollUtil.isEmpty(channelExtDOS)) {
            return null;
        }
        List<ChannelRuleDto> autoPushChannelRules = new ArrayList<>();
        for (ChannelExtDO channelExtDO : channelExtDOS) {
            ChannelRuleDto collectRule = channelExtDO.getCollectRule();
            //开启判断
            if (!CmsSysConstants.STATUS_OPEN.equals(collectRule.getStatus())) {
                continue;
            }
            //判断状态
            if (!collectRule.getContentSourceStatuses().contains(contentDO.getStatus())) {
                continue;
            }
            //创建时间判断
            Date contentCreateTimeBegin = collectRule.getContentCreateTimeBegin();
            if (contentCreateTimeBegin != null && contentDO.getCreateTime().isBefore(DateUtil.toLocalDateTime(contentCreateTimeBegin))) {
                continue;
            }
            final Date contentCreateTimeEnd = collectRule.getContentCreateTimeEnd();
            if (contentCreateTimeEnd != null && contentDO.getCreateTime().isAfter(DateUtil.toLocalDateTime(contentCreateTimeEnd))) {
                continue;
            }
            //标题判断
            //规则标题包含内容标题则满足
            if (CollUtil.isNotEmpty(collectRule.getTitles()) &&
                    CollUtil.isEmpty(
                            CollectionUtils.convertList(collectRule.getTitles(), c -> c.contains(contentDO.getTitle())).stream().filter(c -> c).collect(Collectors.toList())
                    )) {
                continue;
            }
            //规则的目标栏目就是内容所在栏目则跳过此规则，否则一直递归插入数据了
            if (collectRule.getChannelId().equals(contentDO.getChannelId())) {
                continue;
            }
            autoPushChannelRules.add(collectRule);
        }
        return autoPushChannelRules;
    }
}
